/* +-----------------------------------------------------+
 * | Module: fibonacci_number                            |
 * | Description: Calculates the N:th fibonachi number   |
 * | 0, 1, 1, 2, 3, 5, 8, 13 etc.                        |
 * | Input: n, the order number in the series of the     |
 * | number to calculate                                 |
 * | Result: The N:th fibonachi number                   |
 * | Conditions: The input is small enough for the result|
 * | to be possible to store as an integer               |
 * +-----------------------------------------------------+
 */
#include <iostream>
#include <cstdlib>

typedef unsigned int uint_t;

void complexNumber(std::string const &firstNumber, std::string &Real, std::string &Im){
    int number = 0;
    for (uint_t i = 0; i < firstNumber.length(); ++i)
    {
        if (firstNumber.at(i) != ' ')
        {
            if (firstNumber.at(i) == '+' || firstNumber.at(i) == '-')
            {
                ++number;

                if (number == 1)
                {
                    Real = firstNumber.at(i);
                }
                if (number == 2)
                {
                    Im = firstNumber.at(i);
                }
            }
            if (number == 1 && isdigit(firstNumber.at(i)))
            {
                Real += firstNumber.at(i);
            }

            if (number == 2 && isdigit(firstNumber.at(i)))
            {
                Im += firstNumber.at(i);
            }
        }
    }
}

int fibonacciCount(int a_Number)
{
    if ((a_Number == 0) || (a_Number == 1)){    // base cases
      return  a_Number;
    }
    else {
        return fibonacciCount(a_Number - 1) + fibonacciCount(a_Number - 2);
    }
}

int main(int argc, const char* argv[])
{
    std::cout << "----- Menu Lab2 -----" << std::endl;
    std::cout << "1. Fibonacci number (iterative solution)" << std::endl;
    std::cout << "2. Fibonacci number (recursive solution)" << std::endl;
    std::cout << "3. Multiply two complex numbers" << std::endl;
    // std::cout << "4. Pascals triangle (recursive solution)" << std::endl;
    std::cout << "?. Print this menu" << std::endl;
    std::cout << "0. Exit program" << std::endl;
    std::cout << "---------------------" << std::endl;
    char choiseMenu = 0;
    while (choiseMenu != '0'){
        std::cout << "Enter your choice (? gives help): ";
        std::cin >> choiseMenu;
        int numberFibo = 0;
        int firstNumber = 0;
        int nextNumber = 1;
        int currNumber = 0;
        switch (choiseMenu){
            case '1':

                std::cout
                        << "Calculate fibonacci number N iteratively, enter N: ";
                std::cin >> numberFibo;
                for (int i = 0; i < numberFibo; ++i)
                {
                    currNumber = firstNumber + nextNumber;
                    nextNumber = firstNumber;
                    firstNumber = currNumber;
                }
                std::cout << "fibonacci(8) = " << currNumber << std::endl;
                break;
            case '2':
                std::cout
                        << "Calculate fibonacci number N recursively, enter N:";
                std::cin >> numberFibo;
                std::cout << "fibonacci(" << numberFibo << ") = "
                          << fibonacciCount(numberFibo) << std::endl;
                break;
            case '?':
                std::cout << "----- Menu Lab2 -----" << std::endl;
                std::cout << "1. Fibonacci number (iterative solution)"
                          << std::endl;
                std::cout << "2. Fibonacci number (recursive solution)"
                          << std::endl;
                std::cout << "3. Multiply two complex numbers" << std::endl;
                std::cout << "4. Pascals triangle (recursive solution)"
                          << std::endl;
                std::cout << "?. Print this menu" << std::endl;
                std::cout << "0. Exit program" << std::endl;
                std::cout << "---------------------" << std::endl;
                break;
            case '3':
                std::string firstComplexNumber;
                std::string secondComplexNumber;
                std::string firstReal;
                std::string firstIm;
                std::string secIm;
                std::string secReal;
                std::cout << "Calculate (a + bi) * (c + di)" << std::endl;
                std::cout << "Enter a + bi: ";
                getline(std::cin, firstComplexNumber);
                std::cout << "Enter c + di: ";
                getline(std::cin, secondComplexNumber);

                complexNumber(firstComplexNumber, firstReal, firstIm);
                complexNumber(secondComplexNumber, secReal, secIm);

                int a = atoi(firstReal.c_str());
                int b = atoi(firstIm.c_str());
                int c = atoi(secReal.c_str());
                int d = atoi(secIm.c_str());

                std::cout << "(" << firstComplexNumber << ") * (" << secondComplexNumber << ") = (";
                std::cout << (a*c - b*d) << " + " << (b*c + a*d) << "i)";

                break;
            }    // switch
    };    // while
    std::cout << "Bye " << std::endl;
}    // main
